package cn.zm.trip.web.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.json.JSONUtil;
import cn.zm.trip.web.dao.FoodMapper;
import cn.zm.trip.web.domain.Food;
import cn.zm.trip.web.domain.Reply;
import cn.zm.trip.web.domain.WordStatisticsEntity;
import cn.zm.trip.web.domain.Words;
import cn.zm.trip.web.dto.FoodQueryDTO;
import cn.zm.trip.web.service.FoodService;
import cn.zm.trip.web.service.WordsService;
import cn.zm.trip.web.utils.BeanCopyUtils;
import cn.zm.trip.web.vo.FoodVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * 食品控制器
 *
 * @author chen
 * @date 2024/03/19
 */
@Controller
@RequiredArgsConstructor
@RequestMapping(value = "food")
public class FoodController {

    private final FoodService foodService;
    private final WordsService wordsService;
    private final FoodMapper foodMapper;

    /**
     * 跳转首页
     */
    @GetMapping("/index")
    public String index(Model model, FoodQueryDTO foodQueryDTO) {
        List<Food> list;
        if ("comment".equals(foodQueryDTO.getOrderBy())) {
            list = foodMapper.selectListByPage(foodQueryDTO);
        } else {
            //构造查询条件
            QueryWrapper<Food> wrapper = new QueryWrapper<>();
            wrapper.orderByDesc("heat");
            if (CharSequenceUtil.isNotEmpty(foodQueryDTO.getTitle())||
                    CharSequenceUtil.isNotEmpty(foodQueryDTO.getAddress())) {
                wrapper.like("title", foodQueryDTO.getTitle()).or()
                        .like("address", foodQueryDTO.getAddress());
            }
            // 从食品服务获取食品列表
            list = foodService.list(wrapper);
        }

        // 将食品列表添加到模型中，以便在前端页面中访问
        model.addAttribute("foodList", list);
        // 返回视图路径，用于渲染食品列表页面
        return "proscenium/food/index";
    }

    @GetMapping("/content")
    public String content(Model model, Integer id) {
        //封装留言信息
        List<Words> lw_list = wordsService.findByWords();
        model.addAttribute("lw_list", lw_list);

        //封装回复信息
        List<Reply> lr_list = wordsService.findByReply();
        model.addAttribute("lr_list", lr_list);
        // 从食品服务获取食品
        Food food = foodService.getById(id);
        // 将食品列表添加到模型中，以便在前端页面中访问
        FoodVo foodVo = BeanCopyUtils.copyBean(food, FoodVo.class);
        if (CharSequenceUtil.isNotEmpty(foodVo.getPictureUrls())) {
            foodVo.setPictureLists(JSONUtil.toBean(food.getPictureUrls(), new TypeReference<List<String>>() {
            }, true));
        }
        List<WordStatisticsEntity> wordStatisticsEntities = wordsService.listWordStaticsByWordQuery(null, null, id);
        if (CollUtil.isNotEmpty(wordStatisticsEntities)) {
            int sum = wordStatisticsEntities.stream().mapToInt(WordStatisticsEntity::getQuantity).sum();

            for (WordStatisticsEntity statisticsEntity : wordStatisticsEntities) {
                Integer quantity = statisticsEntity.getQuantity();
                double radio = (quantity * 1.0) / sum;
                String proportion = (int) ((radio) * 100) + "%";
                statisticsEntity.setScoreTextByScore();
                statisticsEntity.setScoreRatioText(proportion);
            }
            model.addAttribute("wordStatics", wordStatisticsEntities);
        }

        Food updateEntity = new Food();
        Integer heat = food.getHeat();
        updateEntity.setId(food.getId());
        updateEntity.setHeat(++heat);
        foodService.updateById(updateEntity);

        model.addAttribute("food", foodVo);
        // 返回视图路径，用于渲染食品列表页面
        return "proscenium/food/content";
    }
}
